<?php

class postTable {

    var $page;
    var $article;
    var $cat;
    var $time = null;
    var $body = null;

    function set_page($new_page) {
        $this->pageid = $new_page;
        return $this->pageid;
    }

    function set_id($new_id) {
        $this->id = $new_id;
        return $this->id;
    }

    function set_cat($new_cat) {
        $this->cat = $new_cat;
        return $this->cat;
    }

    function set_tag($new_tag) {
        $this->tag = $new_tag;
        return $this->tag;
    }

    function get_tableTitle() {
        switch($this->pageid) {
            case "1"; //home
                $this->tableTitle = 'News of '.date('l F jS');
                break;

            case "201"; //archive
                if(empty($this->tag)) {
                    $this->tableTitle = 'News about <span class="black">'.$this->cat.'</span>';
                }

                elseif(!empty($this->tag)) {
                    $this->tableTitle = 'News tagged with <span class="black">'.$this->tag.'</span>';
                }
                break;

            case "20"; //drafts
                if(empty($this->tag) && empty($this->cat)) {
                    $this->tableTitle = 'Your drafts';
                }
                break;
        }
        return $this->tableTitle;
    }

    function get_posts_today() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        switch($this->pageid) {

            case "1";
                $query = $connect->query("SELECT id, title, category, time FROM ".$prefix."posts WHERE time > NOW() - INTERVAL 1 DAY AND draft = '0' ORDER BY time DESC");
                break;

            case "201";
                if(!empty($this->cat) && empty($this->tag)) {
                    $query = $connect->query("SELECT id, title, category, time FROM ".$prefix."posts WHERE category = '".$this->cat."' AND draft = '0' ORDER BY time DESC");
                }
                elseif(empty($this->cat) && !empty($this->tag)) {
                    $query = $connect->query("SELECT id, title, tags, time FROM ".$prefix."posts WHERE tags LIKE '%$this->tag%' AND draft = '0' ORDER BY id DESC");
                }
                break;

            case "20";
                if($_SESSION['adminrights'] == true) {
                    $query = $connect->query("SELECT id, draft, author, title, tags, time FROM ".$prefix."posts WHERE author = '".$_SESSION['username']."' AND draft = '1'");
                }
                break;
        }
        $numrows = $query->num_rows;

        $i = 0;

        while ($row = $query->fetch_object()) {
            $commentQuery = $connect->query("SELECT id, postid FROM ".$prefix."comments WHERE page = 'news' AND postid = '".$row->id."'");
            $commentCount = $commentQuery->num_rows;

            $time = date('h:i a',strtotime($row->time));

            $i++;

            if($i % 2) {
                $class = 'odd';
            }
            else {
                $class = 'even';
            }

            $this->postTable[] = array(
                "class" => $class,
                "id" => $row->id,
                "title" => $row->title,
                "category" => $row->category,
                "time" => $time,
                "count" => $commentCount
            );

        }
        return $this->postTable;
        $commentQuery->close();
        $query->close();
        $connect->close();
    }

    function get_lastWeeks_posts() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT id, title, category, time FROM ".$prefix."posts WHERE time > NOW() - INTERVAL 1 WEEK AND draft = '0' ORDER BY time DESC");
        $numrows = $query->num_rows;

        $i = 0;

        while ($row = $query->fetch_object()) {
            $commentQuery = $connect->query("SELECT id, postid FROM ".$prefix."comments WHERE page = 'news' AND postid = '".$row->id."'");
            $commentCount = $commentQuery->num_rows;

            $time = date('m-d',strtotime($row->time));

            $i++;

            if($i % 2) {
                $class = 'odd';
            }
            else {
                $class = 'even';
            }

            $this->lastWeeksNews[] = array(
                "class" => $class,
                "id" => $row->id,
                "title" => $row->title,
                "category" => $row->category,
                "time" => $time,
                "count" => $commentCount
            );

        }
        return $this->lastWeeksNews;
        $commentQuery->close();
        $query->close();
        $connect->close();
    }

    function get_latest_posts() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $query = $connect->query("SELECT id, title, category, time FROM ".$prefix."posts WHERE draft = '0' ORDER BY time DESC LIMIT 20");

        $i = 0;

        while ($row = $query->fetch_object()) {
            $commentQuery = $connect->query("SELECT id, postid FROM ".$prefix."comments WHERE page = 'news' AND postid = '".$row->id."'");
            $commentCount = $commentQuery->num_rows;

            $time = date('m-d',strtotime($row->time));

            $i++;

            if($i % 2) {
                $class = 'odd';
            }
            else {
                $class = 'even';
            }

            $this->latestNews[] = array(
                "class" => $class,
                "id" => $row->id,
                "title" => $row->title,
                "category" => $row->category,
                "time" => $time,
                "count" => $commentCount
            );

        }
        return $this->latestNews;
        $commentQuery->close();
        $query->close();
        $connect->close();
    }
}
?>
